package com.demo.job;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.demo.entity.UserEntity;
import com.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Date;

/**
 * @author wangfengchen
 * 简单任务，实现SimpleJob接口，实现execute方法，里面是具体任务逻辑
 */
@Slf4j
@Component
public class MySimpleJob implements SimpleJob {

  @Autowired
  private UserService userService;


  @Override
  public void execute(ShardingContext shardingContext) {
    LocalTime time = LocalTime.now();
    log.info("time = {}，jobName = {}，分片项 = {}，总分片项 = {}，jobParameter = {}，shardingParameter = {}，taskId = {}",
        time, shardingContext.getJobName(), shardingContext.getShardingItem(), shardingContext.getShardingTotalCount(),
        shardingContext.getJobParameter(), shardingContext.getShardingParameter(), shardingContext.getTaskId());
    UserEntity userEntity = new UserEntity().setId(IdUtil.getSnowflakeNextId())
        .setUsername("test")
        .setPhone("1")
        .setAge(RandomUtil.randomInt(100))
        .setCreateTime(new Date());
    userService.save(userEntity);
  }

}
